package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * 套餐业务实现
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 条件查询
     *
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        return setmealMapper.list(setmeal);
    }

    /**
     * 根据id查询菜品选项
     *
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * @param setmealDTO
     */
    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        // 插入套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);

        // 插入套餐菜品
        for (SetmealDish setmealDish : setmealDTO.getSetmealDishes()) setmealDish.setSetmealId(setmeal.getId());
        setmealDishMapper.insert(setmealDTO.getSetmealDishes());
    }

    /**
     * @param setmeal
     */
    @Override
    public void update(Setmeal setmeal) {
        setmealMapper.update(setmeal);
    }

    /**
     * @param id
     * @return
     */
    @Override
    public SetmealVO query(Integer id) {
        // 查询套餐
        SetmealVO setmealVO = setmealMapper.select(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        // 套餐套餐包含的菜品
        return setmealVO;
    }

    /**
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {

        // 删除缓存

        // 更新套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        // 删除缓存

        // 更新套餐菜品
        setmealDishMapper.deleteBySetmealId(setmeal.getId());
        for (SetmealDish setmealDish : setmealDTO.getSetmealDishes()) setmealDish.setSetmealId(setmeal.getId());
        setmealDishMapper.insert(setmealDTO.getSetmealDishes());
    }

    /**
     * @param ids
     */
    @Override
    public void del(List<Integer> ids) {
        // 删除套餐
        setmealMapper.dels(ids);
        // 删除套餐中的菜品
        setmealDishMapper.delBySetmealIds(ids);
    }

}
